package com.moleq.network;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.R.integer;
import android.util.Xml;

public class CommTaskParser {

	public final static String START_TAG_NAME = "CommTask";
	public final static String TAG_CREATE_DATETIME = "createDatetime";
	public final static String TAG_IPADDRESS = "ipAddress";

	public InputStream readXml(String fileName) throws FileNotFoundException {

		File file = new File(fileName);
		InputStream inputStream = new FileInputStream(file);
		return inputStream;
	}

	public CommTask getCommTasks(String fileName) throws Throwable {
		CommTask commtask = new CommTask();
		InputStream inputStream = null;

		inputStream = readXml(fileName);

		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(inputStream, "UTF-8");

		int eventType = parser.getEventType();
		List<String> actions = new ArrayList<String>();
		while (eventType != XmlPullParser.END_DOCUMENT) {

			switch (eventType) {
			case XmlPullParser.START_DOCUMENT:
				break;
			case XmlPullParser.START_TAG:
				String name = parser.getName();
				if ("CommTask".equals(name)) {
					
//					commtask.setId(parser.getAttributeValue(0));
				}
				if (commtask != null) {
					if ("createDatetime".equals(name)) {
						commtask.setCreateDateTime(parser.nextText());
					}
					if ("station".equals(name)){
						commtask.setStation(Integer.valueOf(parser.nextText()));
					}
					if ("ipAddress".equals(name)) {
						commtask.setIpAddress(parser.nextText());
					}
					if ("dbStructure".equals(name)) {
						commtask.setDbStructure(parser.nextText());
					}
					if("Action".equals(name)){
						actions.add(parser.nextText());
					}
				}
				
				break;
			case XmlPullParser.END_TAG:
//				if ("CommTask".equals(parser.getName())) {
//					tasks.add(commtask);
//					commtask = null;
//				}
				break;
			default:
				break;
			}
			eventType = parser.next();
		}
		commtask.setActions(actions);
		if (inputStream != null)
			inputStream.close();

		return commtask;

	}

	public void save(CommTask tasks, String fileName) throws Throwable {

		File newfile = new File(fileName);
		if (newfile.isDirectory())
			return;

		if (!newfile.exists()) {
			newfile.createNewFile();
		}

		FileWriter writer = new FileWriter(new File(fileName));

		XmlSerializer serializer = Xml.newSerializer();
		serializer.setOutput(writer);
		serializer.startDocument("UTF-8", true);
		serializer.startTag(null, "CommTask");
		serializer.startTag(null,"createDatetime");
		serializer.text(tasks.getCreateDateTime());
		serializer.endTag(null,"createDatetime");
		serializer.startTag(null,"station");
		serializer.text(String.valueOf(tasks.getStation()));
		serializer.endTag(null,"station");
		serializer.startTag(null,"ipAddress");
		serializer.text(tasks.getIpAddress());
		serializer.endTag(null,"ipAddress");
		serializer.startTag(null,"dbStructure");
		serializer.text(tasks.getDbStructure());
		serializer.endTag(null,"dbStructure");
		serializer.startTag(null,"Data");
		
		for(int i = 0;i < tasks.getActions().size();i++){
			serializer.startTag(null,"Action");
			serializer.text(tasks.getActions().get(i));
			serializer.endTag(null,"Action");
		}
		
		serializer.endTag(null,"Data");
//		for (CommTask commTask : tasks) {
//			serializer.startTag(null, "CommTask");
//			serializer.attribute(null, "id", commTask.getId());
//
//			serializer.startTag(null, TAG_CREATE_DATETIME);
//			serializer.text(commTask.getCreateDateTime());
//			serializer.endTag(null, TAG_CREATE_DATETIME);
//
//			serializer.startTag(null, TAG_IPADDRESS);
//			serializer.text(commTask.getIpAddress());
//			serializer.endTag(null, TAG_IPADDRESS);
//
//			serializer.endTag(null, "CommTask");
//		}

		serializer.endTag(null, "CommTask");
		serializer.endDocument();
		writer.flush();
		writer.close();
	}
}
